
#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QGLWidget>
#include <vector>

#include "vectormath_aos.h"
using namespace Vectormath::Aos;


class GLWidget : public QGLWidget
{
	Q_OBJECT

	static const GLdouble FRONT;
	static const GLdouble BACK;
	static const GLdouble SV_TRANSZ;
	static const GLdouble WV_TRANSZ;

	typedef std::vector<Vector3> Vectors;

public:
	enum SHOW { TETRAHEDRON, TETRA_FLAKE, SZENE };

	GLWidget( QWidget *parent = 0 );
    ~GLWidget();

    QSize minimumSizeHint() const;
    QSize sizeHint() const;

	// getter
	SHOW getViewMode() const { return m_show; }
	int getFlakeRecursionDepth() const { return m_flakeRecursionDepth; }
	float getFlakeTetraHeight() const { return m_flakeTetraHeight; }
	float getFlakeTetraHeightAttenuation() const { return m_flakeTetraHeightAttenuation; }
	float getFlakeTetraChildSize() const { return m_flakeTetraChildSize; }

public slots: // slots
	void slotShowTetrahedron(bool checked);
	void slotShowTetraFlake(bool checked);
	void slotShowSzene(bool checked);
	void setFlakeRecursionDepth(int depth);
	void setFlakeTetraHeight(float height);
	void setFlakeTetraHeightAttenuation(float att);
	void setFlakeTetraChildSize(float size);
	void slotGenerateScene(int numberOfObjects);
	void slotSwitchBackgroundColor();

protected:
    virtual void initializeGL();
    virtual void paintGL();
    virtual void resizeGL( int width, int height );
	virtual void mousePressEvent( QMouseEvent *event );
	virtual void mouseMoveEvent( QMouseEvent *event );
    virtual void keyPressEvent ( QKeyEvent * event );

private:
	void drawTetrahedron(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, const Vector3 &p3); // ** TODO **
	void drawTetraFlake(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, int recDepth, float childSize, float height, float heightAttenuation);
	void drawSzene();
	void drawPlane( const Vector3 &p0, const Vector3 &d1, const Vector3 &d2, int numCells );

	// trackball helper functions
	void updateProjectionMatrix(GLdouble front, GLdouble back); // set viewing frustum
	void resetRotation(); // set rotation matrix to Identity


	SHOW			m_show;									//!< MODE: if true show Cube else show whole szene

	// some variables for trackball
	float			m_rotationMatrix[16];					//!< global rotation Matrix
	float			m_xTrans, m_yTrans, m_zTrans;			//!< global translation
    QPoint			m_lastPos;
	Vector3			m_backgroundColor;						//!< background color of openGL window

	// tetrahedron
	Vectors			m_regularTetrahedron;
	// tetrahedron flake parameters
	int				m_flakeRecursionDepth;
	float			m_flakeTetraHeight;						//!< constructing a tetrahedron from a triangle: the height of the 4th vertex
	float			m_flakeTetraHeightAttenuation;
	float			m_flakeTetraChildSize;					//!< size of child tetrahedron relative to parent size \in [0,1]
	// scene
	Vector3			m_plane_point;							//!< plane definition: idx 0 point on plane, idx 1,2 orthogonal direction vectors in plane
	Vector3			m_plane_vectors[2];
	Vectors			m_tetraFlakeCenters;
};

#endif
